/* custom shader includes*/

#pragma once

#include <string>
#include <d3d9.h>
#include <d3dx9.h>

#define SAFE_RELEASE(p)		{ if(p) { (p)->Release(); (p)=NULL; } }

class ScalingEffect
{
    float				m_scale;
    LPCSTR				m_strName;
    std::string			m_strErrors;
    LPDIRECT3DDEVICE9		g_pD3DDev;

    LPD3DXEFFECT			m_pEffect;
    D3DXEFFECT_DESC		m_EffectDesc;

    // Matrix Handles
    D3DXHANDLE m_MatWorldEffectHandle;
    D3DXHANDLE m_MatViewEffectHandle;
    D3DXHANDLE m_MatProjEffectHandle;
    D3DXHANDLE m_MatWorldViewEffectHandle;
    D3DXHANDLE m_MatViewProjEffectHandle;
    D3DXHANDLE m_MatWorldViewProjEffectHandle;

    // Texture Handles
    D3DXHANDLE m_SourceDimsEffectHandle;
    D3DXHANDLE m_TexelSizeEffectHandle;
    D3DXHANDLE m_SourceTextureEffectHandle;
    D3DXHANDLE m_WorkingTextureEffectHandle;

    // Technique stuff
    D3DXHANDLE	m_PreprocessTechniqueEffectHandle;
    D3DXHANDLE	m_CombineTechniqueEffectHandle;

public:
    ScalingEffect(LPDIRECT3DDEVICE9 pD3DDev);
    ~ScalingEffect(void);

    void KillThis();

    HRESULT LoadEffect(const TCHAR *filename);

    const char *getErrors() { return m_strErrors.c_str(); }

    LPCSTR getName() { return m_strName; }
    float getScale() { return m_scale; }

    // Does it have a preprocess step
    BOOL hasPreprocess() { return m_PreprocessTechniqueEffectHandle!=0; }

    // Set The Textures
    HRESULT SetTextures( LPDIRECT3DTEXTURE9 lpSource, LPDIRECT3DTEXTURE9 lpWorking );

    // Set the Matrices for this frame
    HRESULT SetMatrices( D3DXMATRIX &matProj, D3DXMATRIX &matView, D3DXMATRIX &matWorld );

    // Begin the technique
    // Returns Number of passes for this technique
    HRESULT Begin(BOOL bPreprocess, UINT* pPasses);

    // Render Pass in technique
    HRESULT BeginPass(UINT Pass);
    HRESULT EndPass();

    // End rendering of this technique
    HRESULT End();

    // Validates the effect
    HRESULT Validate();

private:
    HRESULT ParseParameters(LPD3DXEFFECTCOMPILER lpEffectCompiler);
};
